const express = require("express");
const mongoose = require("mongoose");
// const fd = require("formidable");
const fs = require("fs");
const bp = require("body-parser");
const router = express.Router();
const app = express();
app.use(bp.urlencoded({ extended: false }));
app.use(router);
app.all("*", function (req, res, next) {
    res.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8080");
    res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
    res.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
    next();
});
app.listen(9911, function () {
    console.log("9911开始监听");
    mongoose.connect('mongodb://localhost/dataBase', {
        useNewUrlParser: true,
        useUnifiedTopology: true
    });
    const schemaObj = new mongoose.Schema({
        uId: String,//用户id
        num: Number,//商品数量
        price: String,//商品价格
        name: String,//商品名字
        sId1: String,//商铺id
        sId2: String,//商品类别id
        sId3: String,//商品id
        ifBug: Boolean,//是否下单
    }, { collection: "products", versionKey: false });
    const myModel = mongoose.model("products", schemaObj);
    const schemaObj1 = new mongoose.Schema({
        name: String,
    }, { collection: "user", versionKey: false });
    const myModel1 = mongoose.model("user", schemaObj1);
    // myModel.create({
    //     uId: '1',
    //     num: '1',
    //     price: '1',
    //     sId1: '1',
    //     sId2: '1',
    //     sId3: '1',
    // }, function (error, result) {
    //     if (!error) {
    //         console.log("插入成功");
    //     } else {
    //         console.log("插入失败");
    //     }
    // });

    router.post("/allPros", function (req, res) {
        //向数据库请求所有数据并返回
        myModel.find(function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        });
    });
    router.post("/findPros", function (req, res) {
        //向查找商品数量
        myModel.find({
            uId: req.body.uid,
            sId1: req.body.shopsId,
            sId2: req.body.categoryId,
            sId3: req.body.itemId,
        }, function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        });
    });
    router.post("/find1Pros", function (req, res) {
        //向查找商品数量
        myModel.find({
            uId: req.body.uid,
            sId1: req.body.shopsId,
        }, function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        });
    });
    router.post("/find2Pros", function (req, res) {
        //向查找商品数量
        myModel.find({
            uId: req.body.uid,
            sId1: req.body.shopsId,
            sId2: req.body.categoryId,
            sId3: req.body.itemId,
        }, function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        });
    });
    router.post("/find3Pros", function (req, res) {
        //通过用户id查找商品
        myModel.find({
            uId: req.body.uid,
        }, function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        });
    });

    router.post("/updPro", function (req, res) {
        myModel.updateOne({ _id: req.body.id }, { $set: { num: req.body.num } }, function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        });
    })
    router.post("/upd1Pro", function (req, res) {//通过商品id更新商品
        myModel.updateOne({ sId3: req.body.id }, { $set: { num: req.body.num } }, function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        });
    })
    router.post("/addPro", function (req, res) {
        //向数据库请求所有数据并返回
        myModel.create({
            uId: req.body.uid,
            num: req.body.num,
            price: req.body.prices,
            sId1: req.body.shopsId,
            name: req.body.name,
            sId2: req.body.categoryId,
            sId3: req.body.itemId,
            ifBug: req.body.ifBug,
        }), function (error, result) {
            if (!error) {
                res.json(result);
            } else {
                res.json(error);
            }
        };
        router.post("/adduser", function (req, res) {
            //向数据库请求所有数据并返回
            myModel1.create({
                name: req.body.username
            }), function (error, result) {
                if (!error) {
                    res.json(result);
                } else {
                    res.json(error);
                }
            };
        });
    });

});
